package com.chdwmp.product.service.impl;


import cn.hutool.core.util.StrUtil;
import com.chdwmp.product.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@SuppressWarnings({"unchecked", "RedundantConditionalExpression"})
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    @Override
    public void setString(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @Override
    public String getString(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public Set<String> getLikeString(String key){
        Set<String> keys = stringRedisTemplate.keys("*" + "small_liquorice_" + key + "*");
        return keys;
    }

    @Override
    public void deleteString(String key) {
        if (!StringUtils.isEmpty(key)) {
            stringRedisTemplate.delete(key);
        }
    }

    @Override
    public void deleteObject(String key) {
        if (!StringUtils.isEmpty(key)) {
            redisTemplate.delete(key);
        }
    }

    @Override
    public void setObject(String key, Object o) {
        redisTemplate.opsForValue().setIfAbsent(key, o);
    }

    @Override
    public Object getObject(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public void setObjectTimeLimit(Object key, Object value, long seconds) {
        ValueOperations<Object, Object> operations = redisTemplate.opsForValue();
        operations.set(key, value, seconds, TimeUnit.SECONDS);
    }

    public void setStringTimeLimit(String key, String value, long seconds) {
        stringRedisTemplate.opsForValue().set(key, value,seconds,TimeUnit.SECONDS);
    }

    public  Long getExpire(String key){
        if (StrUtil.isEmpty(key)){
            return 0L;
        }
        return redisTemplate.getExpire(key);
    }

    @Override
    public void putHashValue(Object key, Object hashKey, Object hashValue) {
        redisTemplate.opsForHash().put(key,hashKey,hashValue);
    }

    @Override
    public Object getHashValue(Object key, Object hashKey) {
        return redisTemplate.opsForHash().get(key,hashKey);
    }

}
